local skel = fk.CreateSkill {
  name = "rmt__quqi",
}

skel:addEffect("active", {
  anim_type = "control",
  card_num = 0,
  min_target_num = 1,
  target_tip = function (self, player, to, selected, selected_cards, card, selectable, extra_data)
    if self.interaction.data == "rmt__quqi_reset" and selectable and table.contains(player:getTableMark("rmt__fenjie_tar"), to.id) then
      return "@@rmt__fenjie_tar"
    end
  end,
  prompt = function (self, player, selected_cards, selected_targets)
    local choice = self.interaction.data
    if choice == "rmt__quqi_reset" then
      return "#rmt__quqi_reset"
    elseif choice == "rmt__quqi_chain" then
      return "#rmt__quqi_chain"
    end
    return "#rmt__quqi"
  end,
  interaction = function (self, player)
    local all_choices = {"rmt__quqi_reset", "rmt__quqi_chain"}
    local choices = table.filter(all_choices, function(ch)
      return player:getMark(ch.."-phase") == 0
    end)
    if #choices > 0 then
      return UI.ComboBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to, selected)
    if self.interaction.data == "rmt__quqi_reset" then
      return to.chained
    else
      return to:getMark("rmt__quqi_hpchange-turn") ~= 0 and not to.chained
    end
  end,
  can_use = function(self, player)
    return player:getMark("rmt__quqi_reset-phase") == 0 or player:getMark("rmt__quqi_chain-phase") == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:sortByAction(effect.tos)
    room:setPlayerMark(player, self.interaction.data.."-phase", 1)
    local recast = self.interaction.data == "rmt__quqi_reset"
    local tos = effect.tos
    for _, to in ipairs(tos) do
      if not to.dead then
        to:setChainState(not recast)
      end
    end
    if not recast then return end
    room:delay(500)
    for _, to in ipairs(tos) do
      if not to.dead then
        room:doIndicate(player, {to})
        local name = "fire__slash"
        if table.contains(player:getTableMark("rmt__fenjie_tar"), to.id) and to:isWounded()
        and room:askToSkillInvoke(player, {skill_name =  skel.name, prompt = "#rmt__quqi-change:"..to.id}) then
          name = "peach"
        end
        room:askToUseVirtualCard(to, {
          name = name, skill_name = skel.name, cancelable = false,
          extra_data = {bypass_times = true}
        })
      end
    end
  end,
})

skel:addEffect(fk.HpChanged, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("rmt__quqi_hpchange-turn") == 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "rmt__quqi_hpchange-turn", 1)
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  local tos = {}
  player.room.logic:getEventsOfScope(GameEvent.ChangeHp, 999, function(e)
    table.insertIfNeed(tos, e.data.who)
  end, Player.HistoryTurn)
  for _, p in ipairs(tos) do
    if not p.dead and p:getMark("rmt__quqi_hpchange-turn") == 0 then
      player.room:setPlayerMark(p, "rmt__quqi_hpchange-turn", 1)
    end
  end
end)

Fk:loadTranslationTable{
  ["rmt__quqi"] = "屈起",
  [":rmt__quqi"] = "出牌阶段各限一次，你可以：1.重置任意名角色，然后令这些角色依次视为使用一张火【杀】（有距离限制）；2.横置任意名本回合体力值变化过的角色。",

  ["#rmt__quqi"] = "屈起：各限一次，重置任意角色，或横置任意名本回合体力值变化过的角色",
  ["#rmt__quqi_reset"] = "屈起：重置任意名角色，然后令这些角色依次视为使用一张火【杀】（有距离限制）",
  ["#rmt__quqi_chain"] = "屈起：横置任意名本回合体力值变化过的角色",
  ["rmt__quqi_reset"] = "重置目标，令其使用火【杀】",
  ["rmt__quqi_chain"] = "横置本回合体力值变化的角色",
  ["@@rmt__fenjie_tar"] = "可吃桃",
  ["#rmt__quqi-change"] = "屈起：%src 即将视为使用火【杀】，你可以令其改为使用【桃】",
}

return skel
